<template>
    <el-dialog title="库存信息" :visible.sync="dialogFormVisible" width="35%" :append-to-body="true" @close="dialogClose">
      <el-form status-icon label-width="100px">
        <el-form-item  label="仓库名称">
            <el-select v-model="stockMasterInfo.fwhid" :disabled="updateOrCreate" placeholder="请选择">
                <el-option
                v-for="item in warehouseList"
                :key="item.fwhid"
                :label="item.fwhname"
                :value="item.fwhid">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="物资名称">
            <el-select v-model="stockMasterInfo.fmid" :disabled="updateOrCreate" placeholder="请选择">
                <el-option
                v-for="item in materialList"
                :key="item.fmid"
                :label="item.fmname"
                :value="item.fmid">
                </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="库存数量">
            <el-input-number v-model="stockMasterInfo.fsmqty" :min="1" :max="99999" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="安全库存数量">
            <el-input-number v-model="stockMasterInfo.fsmsafestockqty" :min="1" :max="99999" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="备注">
              <el-input v-model="stockMasterInfo.fsmmemo" type="textarea" placeholder="请输入内容" maxlength="200"
              show-word-limit  size="medium" autocomplete="off" />
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="submitForm(stockMasterInfo)">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>
</template>
<script>
import { createStockMaster,updateStockMaster } from '@/api/stockMaster'
import { getMaterialList } from '@/api/material'
import { getWarehouseList } from '@/api/warehouse'

export default {
    props: {
        dialogFormVisible: Boolean,
        editStockMasterInfo: {
            type:Array,
            default: []
        },
        updateOrCreate: Boolean
    },
    data() {
        return {
            stockMasterInfo: {},
            materialList: {},
            warehouseList: {}
        }
    },
    created () {
        this.initInfo()
    },
    updated () {
        this.stockMasterInfo = this.editStockMasterInfo
    },
    methods: {
        initInfo(){
            getMaterialList({}).then(response => {
                this.materialList = response.data.materialList
            })
            getWarehouseList({}).then(response => {
                this.warehouseList =response.data.warehouseList
            })
        },
        submitForm(stockMasterInfo) {
            if (this.updateOrCreate) {
                updateStockMaster(stockMasterInfo).then(response => {
                    this.$message.success('修改仓库信息成功')
                    this.$parent.fetchData()
                })
                this.dialogClose()
            } else {
                createStockMaster(stockMasterInfo).then(response => {
                    this.$message.success('创建物资信息成功')
                    this.$parent.fetchData()     
                })
                this.dialogClose()
            }
        },
        resetForm() {
            this.stockMasterInfo = {}
        },
        // 解决父页面按钮只能点击一次问题
        dialogClose() {
            this.dialogFormVisible = false
            this.$parent.vueReset()
        }
    }
}
</script>
    